import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_study_app/module/view/select_group/select_item_view.dart';

class SelectGroupView extends StatefulWidget {
  List<String> optionList;
  List<String> valueList;
  EdgeInsetsGeometry? padding;
  bool isSingleSelect;
  SelectGroupViewController? controller;

  SelectGroupView({
    required this.optionList,
    required this.valueList,
    this.isSingleSelect = true,
    this.padding,
    this.controller,
  });

  @override
  State<StatefulWidget> createState() => _SelectGroupViewState();
}

class _SelectGroupViewState extends State<SelectGroupView> {
  List<String> _selectedItems = [];

  @override
  void initState() {
    super.initState();
    if (widget.controller != null) widget.controller?.attachState(this);
  }

  @override
  void dispose() {
    widget.controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding ?? EdgeInsets.all(12.w),
      child: ListView.separated(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemCount: widget.optionList.length,
        separatorBuilder: (context, index) => SizedBox(
          height: 8.w,
        ),
        itemBuilder: (context, index) {
          bool isSelected = _selectedItems.contains(widget.valueList[index]);
          return SelectItemView(
            isSelected: isSelected,
            isSingleSelect: widget.isSingleSelect,
            text: widget.optionList[index],
            onTap: () {
              String value = widget.valueList[index];
              if (_selectedItems.contains(value)) {
                _selectedItems.remove(value);
                if (widget.controller != null &&
                    widget.controller?.onSelectItem != null) {
                  widget.controller!.onSelectItem!(
                      _selectedItems, widget.isSingleSelect, value, false);
                }
              } else {
                if (widget.isSingleSelect) {
                  if (_selectedItems.isNotEmpty) _selectedItems.clear();
                }
                _selectedItems.add(value);
                if (widget.controller != null &&
                    widget.controller?.onSelectItem != null) {
                  widget.controller!.onSelectItem!(
                      _selectedItems, widget.isSingleSelect, value, true);
                }
              }
              setState(() {});
            },
          );
        },
      ),
    );
  }
}

class SelectGroupViewController {
  _SelectGroupViewState? _state;
  Function(List<String> selectedList, bool isSingle, String item,
      bool isSelected)? _onSelectItem;

  get selectedItems => _state?._selectedItems ?? [];

  get onSelectItem => _onSelectItem;

  addOnSelectItemListener(
      Function(List<String> selectedList, bool isSingle, String item,
              bool isSelected)
          callback) {
    this._onSelectItem = callback;
  }

  attachState(_SelectGroupViewState state) {
    this._state = state;
  }

  dispose() {
    _onSelectItem = null;
    this._state = null;
  }
}
